package com.zxq.ruoyi.network.tcp.notify;
import com.zxq.ruoyi.domain.IotDevice;
import com.zxq.ruoyi.service.impl.IotDeviceServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;

@Service
@Slf4j
public class DeviceStateNotify {

    static DeviceStateNotify deviceStateNotify = null;

    public static DeviceStateNotify getInstance(){

        return deviceStateNotify;
    }

    /**
     * 初始操作
     */
    @PostConstruct
    public void  initialOperation() {
        deviceStateNotify = this;
    }

    @Autowired
    IotDeviceServiceImpl iotDeviceService;


    /**
     * 上线
     * @param devId 设备id
     */
    public void online(String devId){
        log.info(String.format("devId:%s online",devId));
        IotDevice iotDevice = iotDeviceService.selectIotDeviceByDevNum(devId);
        iotDevice.setOnline(1);
        iotDeviceService.updateIotDevice(iotDevice);

    }

    /**
     * 下线
     * @param devId 设备id
     */
    public void offline(String devId){
        log.info(String.format("devId:%s offline",devId));
        IotDevice iotDevice = iotDeviceService.selectIotDeviceByDevNum(devId);
        iotDevice.setOnline(2);
        iotDeviceService.updateIotDevice(iotDevice);

    }

}
